package cn.com.cloudpioneer.evaluate.controller;

import cn.com.cloudpioneer.evaluate.service.FileService;
import cn.com.cloudpioneer.evaluate.utils.CustomException;
import cn.com.cloudpioneer.evaluate.vo.req.ReqFileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @auther maruikai
 * @create 2023/9/13-9:34
 */
@RestController
@RequestMapping("/api/xgs/file")
@Api(tags = "文件管理")
public class FileController extends BaseController {

    @Autowired
    private FileService fileService;

    @RequestMapping(value = "/upload_evaluate", method = RequestMethod.POST)
    @ApiOperation(value = "上传评估文件")
    public Object filesEvaluateUpload(@RequestParam(name = "file") MultipartFile file,
                                      @RequestParam String itemId) throws CustomException {
        fileService.uploadEvaluateFile(itemId, file);
        return "ok";
    }

    @RequestMapping(value = "/upload_target", method = RequestMethod.POST)
    @ApiOperation(value = "上传指标文件")
    public Object filesUpload(@RequestParam(name = "file") MultipartFile file,
                              @RequestParam String targetId) throws CustomException {
        fileService.uploadTargetFile(targetId, file);
        return "ok";
    }

    @RequestMapping(value = "/upload_task", method = RequestMethod.POST)
    @ApiOperation(value = "资料管理文件上传")
    public Object DocumentDataUpload(@RequestParam(name = "file") MultipartFile file,
                                     @RequestParam String taskId) throws CustomException {
        fileService.uploadDocumentData(taskId, file);
        return "ok";
    }

    @RequestMapping(value = "/upload_report", method = RequestMethod.POST)
    @ApiOperation(value = "资料管理文件上传")
    public Object uploadReportFile(@RequestParam(name = "file") MultipartFile file,
                                   @RequestParam String itemId) throws CustomException {
        fileService.uploadReportFile(itemId, file);
        return "ok";
    }

    @RequestMapping(value = "/distribute", method = RequestMethod.POST)
    @ApiOperation(value = "文件分发")
    public Object distributeFile(@RequestParam String itemId) throws CustomException {
        fileService.distributeFile(itemId);
        return "ok";
    }

    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ApiOperation(value = "删除文件")
    public Object delFile(@RequestParam String intactPath, @RequestParam int type) throws CustomException {
        return fileService.delFile(intactPath, type);
    }

    @RequestMapping(value = "/download", method = RequestMethod.POST)
    @ApiOperation(value = "下载文件")
    public Object download(@RequestBody ReqFileVo req) throws CustomException {
        return fileService.downloadFile(req.getIntactPath(), req.getId());
    }

    @RequestMapping(value = "/download_file", method = RequestMethod.GET)
    @ApiOperation(value = "下载文件2")
    public void downloadFile(@RequestParam String basePath,
                             @RequestParam String fileName,
                             @RequestParam String realName,
                             HttpServletResponse response) throws CustomException {
        fileService.download(basePath, fileName, realName, response);
    }

    @RequestMapping(value = "report", method = RequestMethod.GET)
    @ApiOperation(value = "生成报告")
    public Object exReport(@RequestParam String itemId) throws CustomException {
        fileService.exReport(itemId);
        return "ok";
    }

    @RequestMapping(value = "get_doc_list", method = RequestMethod.POST)
    @ApiOperation(value = "获取资源文档列表")
    public Object getFileList(@RequestParam String taskId) throws CustomException {
        return fileService.getFileList(taskId);
    }


    @RequestMapping(value = "get_report_list", method = RequestMethod.POST)
    @ApiOperation(value = "获取资源文档列表")
    public Object getReportFileList(@RequestParam String itemId) throws CustomException {
        return fileService.getReportFileList(itemId);
    }

    @RequestMapping(value = "del_resource", method = RequestMethod.GET)
    @ApiOperation(value = "删除资源/报告文件")
    public Object delResource(@RequestParam long id) throws CustomException {
        fileService.delResource(id);
        return "ok";
    }

}
